#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/1/29 10:33
# @Author : 闫峰
# @File    : get_sku_goor_num.py
import math

from common.setting import ensure_path_sep
from utils.mysqlUtils.db_pool import Database

product_db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "HWY-ZH-GZ-RDS-Product")
warehouse_db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "HWY-ZH-GZ-RDS-Warehouse")

class Inventory:

    def __init__(self, sku_set, wareHouse_id_set,is_local):
        self.env = ''
        self.sku_set = sku_set
        self.wareHouse_id_set = wareHouse_id_set
        self.is_local = is_local

    def get_set(self):
        origin_sql = """SELECT sku,is_group 
        FROM `zhcxkj-center-product`.rb_product 
        WHERE sku in %s;"""
        origin_data = product_db.fetch_all_tuple(origin_sql, (self.sku_set,))
        not_is_group_sku_set = set(map(lambda x:x[0],list(filter(lambda x: x[1] == 0, origin_data))))
        is_group_sku_set = set(map(lambda x: x[0], list(filter(lambda x: x[1] == 1, origin_data))))
        sku_good_num_total = {}
        sku_good_num_dict = {}
        group_sku_res = {}
        if not_is_group_sku_set:
            sku_good_num_dict = self.get_not_is_group_sku_set_good_num(not_is_group_sku_set,self.is_local)
        if is_group_sku_set:
            group_sku_res = self.get_is_group_sku_set_good_num(is_group_sku_set)
        sku_good_num_total.update(sku_good_num_dict)
        sku_good_num_total.update(group_sku_res)
        return sku_good_num_total
    def get_not_is_group_sku_set_good_num(self,not_is_group_sku_set,is_local):
        if is_local:
            get_not_is_group_sku_good_num = f"""
            SELECT sku,good_num good_num 
            FROM `zhcxkj-center-warehouse`.dynamic_inventory 
            WHERE sku in %s
            and warehouse_id in %s
            """
        else:
            get_not_is_group_sku_good_num = f"""
            SELECT sku,good_num good_num 
            FROM `zhcxkj-center-warehouse`.rb_ware_house_product 
            WHERE sku in %s
            and warehouse_id in %s
            """
        sku_good_num_dict = {}

        not_is_group_sku_good_num = warehouse_db.fetch_all_tuple(get_not_is_group_sku_good_num, (not_is_group_sku_set, self.wareHouse_id_set))
        for sku,good_num in not_is_group_sku_good_num:
            if sku not in sku_good_num_dict:
                sku_good_num_dict[sku] = good_num
            else:
                sku_good_num_dict[sku] += good_num
        return sku_good_num_dict
    def get_is_group_sku_set_good_num(self,is_group_sku_set):
        get_child_sku = f"""
        SELECT group_sku,sku,sku_num 
        FROM `zhcxkj-center-product`.rb_product_group_sku 
        WHERE group_sku in %s;
        """
        get_child_sku_res = product_db.fetch_all_tuple(get_child_sku, (is_group_sku_set,))
        group_sku_sales_dict = {}
        for group_sku,sku,sku_num in get_child_sku_res:
            if group_sku not in group_sku_sales_dict:
                group_sku_sales_dict[group_sku] = {sku:sku_num}
            else:
                group_sku_sales_dict[group_sku].update({sku:sku_num})
        child_sku_set = set(map(lambda x:x[1],get_child_sku_res))
        sku_good_num_dict = self.get_not_is_group_sku_set_good_num(child_sku_set,self.is_local)
        group_sku_res ={}
        for group_sku,child_sku_dict in group_sku_sales_dict.items():
            child_sku_sales = []
            for child_sku,child_sku_num in child_sku_dict.items():
                child_sku_sales.append(math.floor(sku_good_num_dict.get(child_sku,0)/child_sku_num))
            min_sales = min(child_sku_sales)
            group_sku_res[group_sku] = min_sales
        return group_sku_res


if __name__ == '__main__':
    a = ('5945572', '17502204', '8826111', '17235116', '14629478')
    a = ('5945572')

    # a =["9458263","5900273","6466490","6587301","6741257","7144781","7214631","7306271","7372213","7379217","7462130","7469656","7493609","9553521","10405939","10411684","10421461","10431617","2296426","11501242","2715187","13094082","9117927","8354853","2242989","13730958","11411023","17442347","9485237","14596455","10577821","4356221","10520826","10614503","14337965","10688670","10486587","17247046","16693911","17312562","10280698","13284319","16020223","13575029","2433395","13999897","14944101","14172825","11405352","13234340"]
    # b = []
    # for sku in a:
    #     if Inventory(sku, {942, 941, 5766}).get_overseas_num() <=5:
    #         print(f"{sku}:{Inventory(sku, {942, 941, 5766}).get_overseas_num()}")
    #         b.append(sku)
    # print(b)
    # print(Inventory(set(a), {5312, 333}).get_local_num())
    # a = ['16683003']
    print(Inventory(set(a), {941, 942,5766,17218},False).get_set())
